package com.jp.knowledge.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/8/21.
 */

public class InfoDataDetailImgAdapter extends RecycleVAdapter<String> {

	private volatile int itemHeight;

	public InfoDataDetailImgAdapter(Context context, @NonNull List<String> list, int itemHeight) {
		super(context, list);
		this.itemHeight = itemHeight;
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.item_info_data_detail_img;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		final ImageView imageView = holder.getImageView(R.id.info_data_detail_img);
		final ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
		if(itemHeight > 0)
			layoutParams.height = itemHeight;
		imageView.setImageResource(R.mipmap.morentutouxiang);
		Glide.with(this.mContext)
				.load(getData(position))
				.asBitmap()
				.diskCacheStrategy(DiskCacheStrategy.SOURCE)
				.into(new ViewTarget<View, Bitmap>(imageView){
					@Override
					public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
						if(resource != null && !resource.isRecycled()) {
							itemHeight = imageView.getMeasuredWidth() * resource.getHeight() / resource.getWidth();
							layoutParams.height = itemHeight;
							imageView.setImageBitmap(resource);
						}
					}
				});
	}
}
